import { matchPath } from '@umijs/max';
import routes from '../../config/router';

export enum HitType {
  FirstWord = "FIRST_WORD",
  Desc = "DESC",
  Global = "GLOBAL",
  EveryFirstWord = "WVERY_FIRST_WORD",
  SingleWord = "SINGLE_WORD"
}

export interface RoutersType {
  name?: string;
  path?: string;
  key?: string;
  routes?: RoutersType[];
  label?: string;
  children?: React.ReactNode;
  component?: string;
  _val?: string;
  firstWordConnect?: string;
  meta?: {
    desc?: string;
    keepAlice?: boolean;
  },
  hitType?: HitType
}

export const findRouterItem = (key: string, _router: RoutersType[] = routes): RoutersType | undefined => {
  let ret: RoutersType | undefined = undefined;
  for (let i = 0; i < _router.length; i++) {
    const item = _router[i];
    if (item.path && matchPath({ path: item.path }, key)) {
      ret = item;
      return ret;
    } else if (item.routes) {
      const res = findRouterItem(key, item.routes);
      if (res) {
        return res;
      }
    }
  }
  return ret;
}